package dxn.nl.file.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import dxn.boot.common.entity.AuthUser;
import dxn.boot.common.entity.CsBasePageQuery;
import dxn.boot.common.entity.CsBasePageResult;
import dxn.nl.file.biz.dao.ExcelImportDao;
import dxn.nl.file.client.model.enums.ExportFileTypeEnum;
import dxn.nl.file.service.CsExportAppModelService;
import dxn.nl.file.service.CsImportFileService;
import dxn.nl.file.biz.manager.MinIoImageManager;
import dxn.nl.file.biz.po.AppModelPo;
import dxn.nl.file.biz.po.FileImportFileDetail;
import dxn.nl.file.biz.vo.CsImportFileBaseVo;
import dxn.nl.file.biz.vo.CsImportFileVo;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
public class CsImportFileServiceImpl implements CsImportFileService {

    @Resource
    private ExcelImportDao excelImportDao;

    @Resource
    private MinIoImageManager minIoImageManager;

    @Resource
    private CsExportAppModelService csExportAppModelService;

    @Value("${cms.bucketName.import}")
    public String bucketName;

    @Override
    public CsBasePageResult<FileImportFileDetail> pageVo(CsBasePageQuery<CsImportFileVo> pageQuery) {
        CsImportFileVo vo = pageQuery.getQuery();
        LambdaQueryWrapper<FileImportFileDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.lt(FileImportFileDetail::getImportTime, vo.getEndTime())
                .gt(FileImportFileDetail::getImportTime, vo.getStartTime());

        if(StringUtils.isNotBlank(vo.getOrgCode()))
        {
            lambdaQueryWrapper.eq(FileImportFileDetail::getOrgCode,vo.getOrgCode());
        }

        if(StringUtils.isNotBlank(vo.getFileTypeCode()))
        {
            lambdaQueryWrapper.eq(FileImportFileDetail::getModeCode,vo.getFileTypeCode());
        }

        if(vo.getStatusCode() != null)
        {
            lambdaQueryWrapper.eq(FileImportFileDetail::getStatusCode,vo.getStatusCode());
        }

        if(StringUtils.isNotBlank(vo.getFileName()))
        {
            lambdaQueryWrapper.like(FileImportFileDetail::getFileName,"%"+vo.getFileName()+"%");
        }

        lambdaQueryWrapper.orderByDesc(FileImportFileDetail::getImportTime);

        IPage page = Page.of(pageQuery.getPageIndex(),pageQuery.getPageSize(),pageQuery.isCheckCount());
        page = excelImportDao.selectPage(page,lambdaQueryWrapper);

        CsBasePageResult<FileImportFileDetail> pageResult = new CsBasePageResult<>();
        pageResult.setRows(page.getRecords());
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    @Override
    public String importFile(MultipartFile file, String fileType, AuthUser userDetails) {

        if (file.isEmpty()) {
            return "上传文件不能为空";
        }

        AppModelPo modelPo = csExportAppModelService.selectByMode(fileType, ExportFileTypeEnum.upload.code);
        if(modelPo == null)
        {
            return "当前模块没有配置导入";
        }

//        CsExportModelUserBind userBind = csExportAppModelService.selectUserBind(vo.getOrgCode(),userDetails.getCode(),vo.getModeCode());
//        if(userBind == null)
//        {
//            return BaseResult.fail("当前用户没有导入权限");
//        }

        FileImportFileDetail po = new FileImportFileDetail();
        po.setFileName(file.getOriginalFilename());
        po.setOrgCode(userDetails.getOrgCode());
        po.setOrgName(userDetails.getOrgName());
        po.setImportOrgCode(userDetails.getOrgCode());
        po.setImportOrgName(userDetails.getOrgName());
        po.setImportUserCode(userDetails.getCode());
        po.setImportUserName(userDetails.getName());
        po.setImportTime(LocalDateTime.now());
        po.setRemark("");
        po.setStatusCode(0);
        po.setVersionNo(0L);

        po.setAppId(modelPo.getAppId());
        po.setModeCode(fileType);
        po.setModeName(modelPo.getModeName());

        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd/HHmmss-");
        LocalDateTime time = LocalDateTime.now();
        String localTime = df.format(time);


        String imagePath = fileType+"/"+po.getOrgCode()+"/"+localTime+file.getOriginalFilename();

        //po.setFilePath(FileSysTypeEnum.cs_import.getBucketName()+"/"+imagePath);
        po.setFilePath(imagePath);

        //上传文件
        String result = minIoImageManager.saveFile(bucketName,file,imagePath);
        if(StringUtils.isNotBlank(result))
        {
            return result;
        }

        excelImportDao.insert(po);

        return null;
    }

    @Override
    public String pushMq(CsImportFileBaseVo vo) {
        int result = excelImportDao.updateCanConsume(vo);
        if(result > 0)
        {
            return null;
        }
        return "修改失败";
    }

    @Override
    public String deleteFile(CsImportFileBaseVo vo) {
        int result = excelImportDao.deleteById(vo.getFileId());
        if(result > 0)
        {
            return null;
        }
        return "删除失败";
    }
}
